﻿using System.Collections.Concurrent;
using System.Diagnostics;
using Utils;

namespace Test101
{
    internal class Program
    {
        static int num = 0;
        static ConcurrentDictionary<int, object> dict = new ConcurrentDictionary<int, object>();

        static void Main(string[] args)
        {
            CalcSpeed();

            for (int i = 0; i < 500; i++)
            {
                Request(i);
            }

            Console.WriteLine($"Main函数结束");
            Console.ReadLine();
        }

        static void Request(int index)
        {
            dict.TryAdd(index, null);

            while (true)
            {
                string url = "http://localhost:5028/Test/TestGet";
                string result = HttpUtil.HttpGet(url);
                Interlocked.Increment(ref num);
            }
        }

        static void CalcSpeed()
        {
            _ = Task.Factory.StartNew(() =>
            {
                Stopwatch sw = Stopwatch.StartNew();
                while (true)
                {
                    Thread.Sleep(2000);
                    double speed = num / sw.Elapsed.TotalSeconds;
                    ThreadPool.GetMaxThreads(out int w1, out int c1);
                    ThreadPool.GetAvailableThreads(out int w2, out int c2);
                    Console.WriteLine($"有 {dict.Count.ToString().PadLeft(3)} 个 while(true) 在执行，线程池活动线程数：{(w1 - w2).ToString().PadRight(3)}  速度：{speed:#### ####.0} 次/秒");
                }
            }, TaskCreationOptions.LongRunning);
        }
    }
}
